// pages/index/discussion/message/message.js
import Dialog from "../../../../miniprogram_npm/@vant/weapp/dialog/dialog"
import Toast from "../../../../miniprogram_npm/@vant/weapp/toast/toast"

const app = getApp()
const log = new app.appLogs()
const http = new app.appHttp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        bouleuterion_id: "",
        msg_content: "",
        fileList: [],
        maxNumber: 200,//可输入最大字数
        number: 0,//已输入字数
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            bouleuterion_id: options.id
        })
        log.e(options.id)
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    /**
     * 内容
     */
    getTextareaValue: function (e) {
        this.setData({
            msg_content: e.detail.value,
            number: e.detail.value.length//获取textarea的内容长度
        });
    },
    /**
     * 图片上传
     */
    afterRead: function (e) {
        const than = this
        for (let i = 0; i < e.detail.file.length; i++) {
            than.data.fileList.push({
                url: e.detail.file[i].path,
                name: e.detail.file[i].size
            })
        }
        than.setData({
            fileList: than.data.fileList
        })
    },
    /**
     * 删除图片
     */
    deleteImage: function (e) {
        const than = this
        const index = e.detail.index
        const deleteData = this.data.fileList.splice(index, 1)
        than.setData({
            fileList: than.data.fileList
        })
    },
    /**
     * 上传
     * @param e
     */
    setUp: function (e) {
        const than = this
        const fileListproperty = than.data.fileList
        const img_url = []
        let imgurl = ""
        //上传附件
        if (fileListproperty != null) {
            for (let i = 0; i < fileListproperty.length; i++) {
                http.upFile(fileListproperty[i].url, "property")
                    .then((res) => {
                        img_url[i] = res.data.url
                        imgurl += res.data.url + ","
                        log.e(JSON.stringify(img_url))
                        log.e(imgurl)
                    })
            }
        }

        if (than.data.msg_content != null) {
            Dialog.confirm({
                title: "留言",
                message: "确定要留言参与议事吗？"
            }).then(() => {
                //确认按钮
                Toast.loading({
                    duration: 2000,
                    forbidClick: true,
                    message: '正在上报，请稍等！'
                })
                http.postJson("bouleuterion_message/save", {
                    bouleuterion_id: than.data.bouleuterion_id,
                    msg_content: than.data.msg_content,
                    img_url: imgurl,
                }, {}).then((res) => {
                    if (res.data.code == 200) {
                        Toast.success({
                            message: res.data.msg
                        })
                        setTimeout(() => {
                            wx.navigateBack({
                                delta: 2
                            })
                        }, 2000)
                    }
                })
            })
        } else {
            Toast.fail({
                message: '请输入留言内容！'
            })
        }
    }
})